package com.java.admin.message.service;

import com.java.admin.message.entity.MyMessage;
import com.java.database.JunUtil;
import com.java.database.entity.Message;
import com.java.database.entity.PagingEntity;
import com.java.database.mapper.MessageMapper;
import com.java.database.mapper.MessageappendMapper;
import com.java.database.mapper.MessagereplyMapper;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageappendMapper messageappendMapper;
    @Autowired
    private MessagereplyMapper messagereplyMapper;

    /**
     *  分页查询所有留言信息
     */
    public PagingEntity queryAllMessage(String pageNos,String like){
        //  得到页码
        int pageNo = 1;
        try{
            pageNo = Integer.parseInt(pageNos);
        }catch(Exception e){
            pageNo = 1;
        }

        if(JunUtil.ifNull(like)){
            like = "";
        }
        //  查询所有数据
        List<Message> list = messageMapper.selectMessageByLike(like);


        //  分页
        JunUtil jun = new JunUtil();
        jun.setAllData(list);
        jun.setPageNo(pageNo);
        jun.setPageSzie(5);

        //  将分页数据封装到分页对象中
        PagingEntity pagingEntity = new PagingEntity();
        jun.change(pagingEntity,jun,like);

        List<MyMessage> allMessageLists = new ArrayList<MyMessage>();

        for(int i =0;i<pagingEntity.getList().size();i++){
            Message message = (Message) pagingEntity.getList().get(i);
            MyMessage myMessage = new MyMessage();
            myMessage.setMessageId(message.getMessageid());
            myMessage.setName(message.getMessagewritename());
            myMessage.setPhone(message.getMessagewritephone());
            //  根据留言编号查询留言内容
            myMessage.setMessageappends(messageappendMapper.queryMessaggeAppendByMessageId(message.getMessageid()));
            //  根据留言编号查询回复内容
            myMessage.setMessagereplies(messagereplyMapper.queryMessagereplyByMessageId(message.getMessageid()));

            allMessageLists.add(myMessage);
        }
        pagingEntity.setList(allMessageLists);

        return pagingEntity;
    }
}
